<?php
/**
 * 点大商城（www.diandashop.com） - 微信公众号小程序商城系统!
 * Copyright © 2020 山东点大网络科技有限公司 保留所有权利
 * =========================================================
 * 版本：V2
 * 授权主体：shop.guanjunbang.cn
 * 授权域名：shop.guanjunbang.cn
 * 授权码：vdcajlVLIVAgVdkfJvgY
 * ----------------------------------------------
 * 您只能在商业授权范围内使用，不可二次转售、分发、分享、传播
 * 任何企业和个人不得对代码以任何目的任何形式的再发布
 * =========================================================
 */

//管理员中心 -- 核销订单
namespace app\controller;
use think\facade\Db;

class ApiAdminHexiao extends ApiAdmin {

	public function getDay() {
		$start_date = input('post.start_date');
		$end_date = input('post.end_date');
		$time = strtotime($end_date) - strtotime($start_date);

		$day = $time / 86400 + 1;

		return $this->json(['status' => 1, 'day' => $day]);
	}

	//确定租赁
	public function hexiao0() {
		$type = input('param.type');
		$code = input('param.co');
		$start_date = input('param.start_date');

		//多商户可以核销平台的商品
		$plat_hexiao = false;
		if ($type == 'yuyue') {
			$order = db($type . '_order')->where(['aid' => aid, 'hexiao_code' => $code])->find();
			if ($order['bid'] != bid && !$plat_hexiao && !getcustom('mendian_hexiao_givemoney')) {
				return $this->json(['status' => 2, 'msg' => '登录的账号不是该商家的管理账号，是否切换账号？']);
			}

			if (!$order) {
				return $this->json(['status' => 0, 'msg' => '订单不存在']);
			}

			if ($order['status'] == 0) {
				return $this->json(['status' => 0, 'msg' => '订单未支付']);
			}

			if ($order['status'] == 3) {
				return $this->json(['status' => 0, 'msg' => '订单已核销']);
			}

			if ($order['status'] == 4) {
				return $this->json(['status' => 0, 'msg' => '订单已关闭']);
			}

			$order['createtime'] = date('Y-m-d H:i:s', $order['createtime']);
			$order['paytime'] = date('Y-m-d H:i:s', $order['paytime']);
			$member = Db::name('member')->where('id', $order['mid'])->field('id,nickname,headimg')->find();
			$order['nickname'] = $member['nickname'];
			$order['headimg'] = $member['headimg'];
		}

		if ($this->user['isadmin'] == 0) {
			$auth_data = json_decode($this->user['hexiao_auth_data'], true);
			if (!in_array($type, $auth_data)) {
				//return $this->json(['status' => 0, 'msg' => '您没有核销权限']);
			}
			if ($type == 'shop' || $type == 'collage' || $type == 'kanjia' || $type == 'scoreshop' || $type == 'cycle') {
				if ($this->user['mdid'] != 0 && $this->user['mdid'] != $order['mdid'] && !$plat_hexiao) {
					//return $this->json(['status' => 0, 'msg' => '您没有该门店核销权限']);
				}
			}
		}

		if (input('post.op') == 'confirm') {
			$typeArr = ['shop', 'collage', 'lucky_collage', 'kanjia', 'scoreshop', 'seckill', 'yueke', 'restaurant_shop', 'restaurant_takeaway', 'tuangou'];

			if ($type == 'yuyue') {
				$hexiao = Db::name('hexiao_order')->where('aid', aid)
					->where('bid', bid)->where('mid', $order['mid'])->where('orderid', $order['id'])->find();
				if ($hexiao) {
					return $this->json(['status' => 0, 'msg' => '该订单已核销']);
				}

				//更新订单状态
				Db::name('yuyue_order')->where('id', $order['id'])->update(['order_status' => 1, 'start_date' => input('post.start_date')]); //核销后，服务中

			}
			return $this->json(['status' => 1, 'msg' => '确认成功']);
		}
		return $this->json(['order' => $order, 'status' => 1, 'start_date' => $start_date]);
	}
	//核销-完成
	public function hexiao1() {
		$type = input('param.type');
		$code = input('param.co');
		$refund_price = input('param.refund_price');
		$day = input('param.day');
		$end_date = input('param.end_date');

		//多商户可以核销平台的商品
		$plat_hexiao = false;
		if ($type == 'yuyue') {
			$order = db($type . '_order')->where(['aid' => aid, 'hexiao_code' => $code])->find();
			if ($order['bid'] != bid && !$plat_hexiao && !getcustom('mendian_hexiao_givemoney')) {
				return $this->json(['status' => 2, 'msg' => '登录的账号不是该商家的管理账号，是否切换账号？']);
			}

			if (!$order) {
				return $this->json(['status' => 0, 'msg' => '订单不存在']);
			}

			if ($order['status'] == 0) {
				return $this->json(['status' => 0, 'msg' => '订单未支付']);
			}

			if ($order['status'] == 3) {
				return $this->json(['status' => 0, 'msg' => '订单已核销']);
			}

			if ($order['status'] == 4) {
				return $this->json(['status' => 0, 'msg' => '订单已关闭']);
			}

			if ($type == 'shop' || $type == 'scoreshop') {
				$order['prolist'] = Db::name($type . '_order_goods')->where(['orderid' => $order['id']])->select()->toArray();
			} elseif ($type == 'lucky_collage') {
				$order['prolist'] = [['name' => $order['proname'], 'pic' => $order['propic'], 'ggname' => $order['ggname'], 'sell_price' => $order['sell_price'], 'num' => $order['num']]];
			} else {
				$order['prolist'] = [['name' => $order['proname'], 'pic' => $order['propic'], 'ggname' => $order['ggname'], 'sell_price' => $order['sell_price'], 'num' => $order['num']]];
			}
			if ($order['fwtype'] == 1) {

				$order['storeinfo'] = Db::name('mendian')
					->where('id', $order['mdid'])->field('name,address,longitude,latitude')->find();
			}
			$order['createtime'] = date('Y-m-d H:i:s', $order['createtime']);
			$order['paytime'] = date('Y-m-d H:i:s', $order['paytime']);
			$member = Db::name('member')->where('id', $order['mid'])->field('id,nickname,headimg')->find();
			$order['nickname'] = $member['nickname'];
			$order['headimg'] = $member['headimg'];
		} else {
			$order = db($type . '_order')->where(['aid' => aid, 'hexiao_code' => $code])->find();
			if ($order['bid'] != bid && !$plat_hexiao && !getcustom('mendian_hexiao_givemoney')) {
				return $this->json(['status' => 2, 'msg' => '登录的账号不是该商家的管理账号，是否切换账号？']);
			}

			if (!$order) {
				return $this->json(['status' => 0, 'msg' => '订单不存在']);
			}

			if ($order['status'] == 0) {
				return $this->json(['status' => 0, 'msg' => '订单未支付']);
			}

			if ($order['status'] == 3) {
				return $this->json(['status' => 0, 'msg' => '订单已核销']);
			}

			if ($order['status'] == 4) {
				return $this->json(['status' => 0, 'msg' => '订单已关闭']);
			}

			if ($type == 'shop' || $type == 'scoreshop') {
				$order['prolist'] = Db::name($type . '_order_goods')->where(['orderid' => $order['id']])->select()->toArray();
			} elseif ($type == 'lucky_collage') {
				$order['prolist'] = [['name' => $order['proname'], 'pic' => $order['propic'], 'ggname' => $order['ggname'], 'sell_price' => $order['sell_price'], 'num' => $order['num']]];
			} else {
				$order['prolist'] = [['name' => $order['proname'], 'pic' => $order['propic'], 'ggname' => $order['ggname'], 'sell_price' => $order['sell_price'], 'num' => $order['num']]];
			}
			if ($order['freight_type'] == 1) {
				$order['storeinfo'] = Db::name('mendian')->where('id', $order['mdid'])->field('name,address,longitude,latitude')->find();
			}

			$member = Db::name('member')->where('id', $order['mid'])->field('id,nickname,headimg')->find();
			$order['nickname'] = $member['nickname'];
			$order['headimg'] = $member['headimg'];
		}

		if ($this->user['isadmin'] == 0) {
			$auth_data = json_decode($this->user['hexiao_auth_data'], true);
			if (!in_array($type, $auth_data)) {
				//return $this->json(['status' => 0, 'msg' => '您没有核销权限']);
			}
			if ($type == 'shop' || $type == 'collage' || $type == 'kanjia' || $type == 'scoreshop' || $type == 'cycle') {
				if ($this->user['mdid'] != 0 && $this->user['mdid'] != $order['mdid'] && !$plat_hexiao) {
					//return $this->json(['status' => 0, 'msg' => '您没有该门店核销权限']);
				}
			}
		}

		if (input('post.op') == 'confirm') {
			$typeArr = ['shop', 'collage', 'lucky_collage', 'kanjia', 'scoreshop', 'seckill', 'yueke', 'restaurant_shop', 'restaurant_takeaway', 'tuangou'];

			if ($type == 'yuyue') {
				$hexiao = Db::name('hexiao_order')->where('aid', aid)
					->where('bid', bid)->where('mid', $order['mid'])->where('orderid', $order['id'])->find();
				if ($hexiao) {
					return $this->json(['status' => 0, 'msg' => '该订单已核销']);
				}

				$data = array();
				$data['aid'] = aid;
				$data['bid'] = bid;
				$data['uid'] = $this->uid;
				$data['mid'] = $order['mid'];
				$data['orderid'] = $order['id'];
				$data['ordernum'] = date('ymdHis') . aid . rand(1000, 9999);
				$data['title'] = $order['proname'];
				$data['type'] = $type;
				$data['createtime'] = time();
				$data['remark'] = '核销员[' . $this->user['un'] . ']核销';
				Db::name('hexiao_order')->insert($data);

				if ($refund_price > 0) {
					//退款

					if ($order['status'] != 1 && $order['status'] != 2) {
						return json(['status' => 0, 'msg' => '该订单状态不允许退款']);
					}
					$rs = \app\common\Order::refund($order, $refund_price, '租赁到期，退回剩余押金');
					if ($rs['status'] == 0) {
						return json(['status' => 0, 'msg' => $rs['msg']]);
					}

					// Db::name('yuyue_order')->where('id', $orderid)->where('aid', aid)->update(['status' => 4, 'refund_status' => 2]);

					//取消配送订单
					//Db::name('yuyue_worker_order')->where('id', $order['worker_orderid'])->update(['status' => -1]);

					//积分抵扣的返还
					if ($order['scoredkscore'] > 0) {
						\app\common\Member::addscore(aid, $order['mid'], $order['scoredkscore'], '订单退款返还');
					}
					$business_name = Db::name('business')->where('id', $order['bid'])->value('name');
					//退款成功通知
					$tmplcontent = [];
					$tmplcontent['character_string1'] = $order['ordernum'];
					$tmplcontent['amount2'] = $order['refund_money'];
					$tmplcontent['time4'] = $order['refund_time'];
					$tmplcontent['thing5'] = $business_name;
					$tmplcontent['thing3'] = $order['title'];

					\app\common\Wechat::sendtmpl(aid, $order['mid'], '0bS1cJVoipcXmdjKPTka7rpU1OYt8EBKkDPgp8xpMuE', $tmplcontent, m_url('pages/my/usercenter'));

					//订阅消息
					$tmplcontent = [];
					$tmplcontent['amount6'] = $refund_price;
					$tmplcontent['thing3'] = $order['title'];
					$tmplcontent['character_string2'] = $order['ordernum'];
					$tmplcontentnew = [];
					$tmplcontentnew['amount3'] = $refund_price;
					$tmplcontentnew['thing6'] = $order['title'];
					$tmplcontentnew['character_string4'] = $order['ordernum'];
					\app\common\Wechat::sendwxtmpl(aid, $order['mid'], 'tmpl_tuisuccess', $tmplcontentnew, 'pages/my/usercenter', $tmplcontent);

					//短信通知
					$member = Db::name('member')->where('id', $order['mid'])->find();
					if ($member['tel']) {
						$tel = $member['tel'];
					} else {
						$tel = $order['tel'];
					}
					$rs = \app\common\Sms::send(aid, $tel, 'tmpl_tuisuccess', ['ordernum' => $order['ordernum'], 'money' => $refund_price]);

				}

				//更新订单状态
				Db::name('yuyue_order')->where('id', $order['id'])->update(['status' => 2, 'order_status' => 2, 'day' => $day, 'end_date' => $end_date,'refund_status'=>2, 'refund_money' => $refund_price]); //核销后，服务中

			}
			return $this->json(['status' => 1, 'msg' => '核销成功']);
		}
		return $this->json(['order' => $order, 'status' => 1]);
	}

	//核销
	public function hexiao() {
		$type = input('param.type');
		$code = input('param.co');

		//多商户可以核销平台的商品
		$plat_hexiao = false;
		if ($type == 'coupon') {
			$order = Db::name('coupon_record')->where('aid', aid)->where('code', $code)->find();
			if ($order['bid'] != bid && !$plat_hexiao && !getcustom('mendian_hexiao_givemoney')) {
				return $this->json(['status' => 0, 'msg' => '登录的账号不是该商家的管理账号']);
			}

			if (!$order) {
				return $this->json(['status' => 0, 'msg' => t('优惠券') . '不存在']);
			}

			if ($order['status'] == 1) {
				return $this->json(['status' => 0, 'msg' => t('优惠券') . '已使用']);
			}

			if ($order['starttime'] > time()) {
				return $this->json(['status' => 0, 'msg' => t('优惠券') . '尚未生效']);
			}

			if ($order['endtime'] < time()) {
				return $this->json(['status' => 0, 'msg' => t('优惠券') . '已过期']);
			}

			if ($order['type'] == 3 && $order['used_count'] >= $order['limit_count']) {
				return $this->json(['status' => 0, 'msg' => '已达到使用次数']);
			}

			if ($order['type'] == 3 && $order['limit_perday'] > 0) {
				//是否达到每天使用次数限制
				$dayhxnum = Db::name('hexiao_order')->where('orderid', $order['id'])->where('type', 'coupon')->where('createtime', 'between', [strtotime(date('Y-m-d 00:00:00')), strtotime(date('Y-m-d 23:59:59'))])->count();
				if ($dayhxnum >= $order['limit_perday']) {
					return $this->json(['status' => 0, 'msg' => '该计次券每天最多核销' . $order['limit_perday'] . '次']);
				}
			}

			$coupon = Db::name('coupon')->where('id', $order['couponid'])->find();
			$order['usetips'] = $coupon['usetips'];
			$order['createtime'] = date('Y-m-d H:i:s', $order['createtime']);
			$order['usetime'] = date('Y-m-d H:i:s', $order['usetime']);
			$order['starttime'] = date('Y-m-d H:i:s', $order['starttime']);
			$order['endtime'] = date('Y-m-d H:i:s', $order['endtime']);
		} elseif ($type == 'cycle') {
			$order_state = db('cycle_order_stage')->where(['aid' => aid, 'hexiao_code' => $code])->find();
			if ($order_state['bid'] != bid && !$plat_hexiao) {
				return $this->json(['status' => 2, 'msg' => '登录的账号不是该商家的管理账号，是否切换账号？']);
			}

			if (!$order_state) {
				return $this->json(['status' => 0, 'msg' => '订单不存在']);
			}

			if ($order_state['status'] == 0) {
				return $this->json(['status' => 0, 'msg' => '订单未支付']);
			}

			if ($order_state['status'] == 3) {
				return $this->json(['status' => 0, 'msg' => '订单已核销']);
			}

			$order = db('cycle_order')->where(['aid' => aid, 'id' => $order_state['orderid']])->find();
			$order['prolist'] = [['name' => $order['proname'], 'pic' => $order['propic'], 'ggname' => $order['ggname'], 'sell_price' => $order['sell_price'], 'num' => $order['num']]];
			$order['stage'] = $order_state;
			if ($order['freight_type'] == 1) {
				$order['storeinfo'] = Db::name('mendian')->where('id', $order['mdid'])->field('name,address,longitude,latitude')->find();
			}
			$member = Db::name('member')->where('id', $order['mid'])->field('id,nickname,headimg')->find();
			$order['createtime'] = date('Y-m-d H:i:s', $order['createtime']);
			$order['paytime'] = date('Y-m-d H:i:s', $order['paytime']);
			$order['nickname'] = $member['nickname'];
			$order['headimg'] = $member['headimg'];

		} elseif ($type == 'choujiang') {
			if (bid != 0 && !$plat_hexiao && !getcustom('mendian_hexiao_givemoney')) {
				return $this->json(['status' => 0, 'msg' => '登录的账号不是该商家的管理账号']);
			}

			$order = Db::name('choujiang_record')->where(['aid' => aid, 'code' => $code])->find();
			if (!$order) {
				return $this->json(['status' => 0, 'msg' => '奖品不存在']);
			}

			if ($order['status'] == 1) {
				return $this->json(['status' => 0, 'msg' => '奖品已兑换']);
			}

			$order['formdata'] = json_decode($order['formdata'], true);
		} elseif ($type == 'shopproduct') {
			$og = Db::name('shop_order_goods')->where('aid', aid)->where('hexiao_code', $code)->find();
			if (!$og) {
				return $this->json(['status' => 0, 'msg' => '核销码已失效']);
			}

			$order = Db::name('shop_order')->where('aid', aid)->where('id', $og['orderid'])->find();
			if (!$order) {
				return $this->json(['status' => 0, 'msg' => '订单已删除']);
			}

			if ($order['bid'] != bid) {
				$freight = Db::name('freight')->where('aid', aid)->where('id', $order['freight_id'])->find();
				if ($freight['hxbids'] && !in_array(bid, explode(',', $freight['hxbids']))) {
					return $this->json(['status' => 0, 'msg' => '登录的账号不能核销平台商品']);
				}
			}
			$order['createtime'] = date('Y-m-d H:i:s', $order['createtime']);
			$order['paytime'] = date('Y-m-d H:i:s', $order['paytime']);

			$order['ogdata'] = $og;
			$order['hxnum'] = input('param.hxnum');

			if ($order['hxnum'] > $og['num'] - $og['hexiao_num']) {
				return $this->json(['status' => 0, 'msg' => '可核销数量不足']);
			}

		} elseif ($type == 'yuyue') {
			$order = db($type . '_order')->where(['aid' => aid, 'hexiao_code' => $code])->find();
			if ($order['bid'] != bid && !$plat_hexiao && !getcustom('mendian_hexiao_givemoney')) {
				return $this->json(['status' => 2, 'msg' => '登录的账号不是该商家的管理账号，是否切换账号？']);
			}

			if (!$order) {
				return $this->json(['status' => 0, 'msg' => '订单不存在']);
			}

			if ($order['status'] == 0) {
				return $this->json(['status' => 0, 'msg' => '订单未支付']);
			}

			if ($order['status'] == 3) {
				return $this->json(['status' => 0, 'msg' => '订单已核销']);
			}

			if ($order['status'] == 4) {
				return $this->json(['status' => 0, 'msg' => '订单已关闭']);
			}

			if ($type == 'shop' || $type == 'scoreshop') {
				$order['prolist'] = Db::name($type . '_order_goods')->where(['orderid' => $order['id']])->select()->toArray();
			} elseif ($type == 'lucky_collage') {
				$order['prolist'] = [['name' => $order['proname'], 'pic' => $order['propic'], 'ggname' => $order['ggname'], 'sell_price' => $order['sell_price'], 'num' => $order['num']]];
			} else {
				$order['prolist'] = [['name' => $order['proname'], 'pic' => $order['propic'], 'ggname' => $order['ggname'], 'sell_price' => $order['sell_price'], 'num' => $order['num']]];
			}
			if ($order['fwtype'] == 1) {

				$order['storeinfo'] = Db::name('mendian')
					->where('id', $order['mdid'])->field('name,address,longitude,latitude')->find();
			}
			$order['createtime'] = date('Y-m-d H:i:s', $order['createtime']);
			$order['paytime'] = date('Y-m-d H:i:s', $order['paytime']);
			$member = Db::name('member')->where('id', $order['mid'])->field('id,nickname,headimg')->find();
			$order['nickname'] = $member['nickname'];
			$order['headimg'] = $member['headimg'];
		} else {
			$order = db($type . '_order')->where(['aid' => aid, 'hexiao_code' => $code])->find();
			if ($order['bid'] != bid && !$plat_hexiao && !getcustom('mendian_hexiao_givemoney')) {
				return $this->json(['status' => 2, 'msg' => '登录的账号不是该商家的管理账号，是否切换账号？']);
			}

			if (!$order) {
				return $this->json(['status' => 0, 'msg' => '订单不存在']);
			}

			if ($order['status'] == 0) {
				return $this->json(['status' => 0, 'msg' => '订单未支付']);
			}

			if ($order['status'] == 3) {
				return $this->json(['status' => 0, 'msg' => '订单已核销']);
			}

			if ($order['status'] == 4) {
				return $this->json(['status' => 0, 'msg' => '订单已关闭']);
			}

			if ($type == 'shop' || $type == 'scoreshop') {
				$order['prolist'] = Db::name($type . '_order_goods')->where(['orderid' => $order['id']])->select()->toArray();
			} elseif ($type == 'lucky_collage') {
				$order['prolist'] = [['name' => $order['proname'], 'pic' => $order['propic'], 'ggname' => $order['ggname'], 'sell_price' => $order['sell_price'], 'num' => $order['num']]];
			} else {
				$order['prolist'] = [['name' => $order['proname'], 'pic' => $order['propic'], 'ggname' => $order['ggname'], 'sell_price' => $order['sell_price'], 'num' => $order['num']]];
			}
			if ($order['freight_type'] == 1) {
				$order['storeinfo'] = Db::name('mendian')->where('id', $order['mdid'])->field('name,address,longitude,latitude')->find();
			}

			$member = Db::name('member')->where('id', $order['mid'])->field('id,nickname,headimg')->find();
			$order['nickname'] = $member['nickname'];
			$order['headimg'] = $member['headimg'];
		}

		if ($this->user['isadmin'] == 0) {
			$auth_data = json_decode($this->user['hexiao_auth_data'], true);
			if (!in_array($type, $auth_data)) {
				//return $this->json(['status' => 0, 'msg' => '您没有核销权限']);
			}
			if ($type == 'shop' || $type == 'collage' || $type == 'kanjia' || $type == 'scoreshop' || $type == 'cycle') {
				if ($this->user['mdid'] != 0 && $this->user['mdid'] != $order['mdid'] && !$plat_hexiao) {
					//return $this->json(['status' => 0, 'msg' => '您没有该门店核销权限']);
				}
			}
		}

		if (input('post.op') == 'confirm') {
			$typeArr = ['shop', 'collage', 'lucky_collage', 'kanjia', 'scoreshop', 'seckill', 'yueke', 'restaurant_shop', 'restaurant_takeaway', 'tuangou'];

			if (in_array($type, $typeArr)) {
				$data = array();
				$data['aid'] = aid;
				$data['bid'] = bid;
				$data['uid'] = $this->uid;
				$data['mid'] = $order['mid'];
				$data['orderid'] = $order['id'];
				$data['ordernum'] = $order['ordernum'];
				$data['title'] = $order['title'];
				$data['type'] = $type;
				$data['createtime'] = time();
				$data['remark'] = '核销员[' . $this->user['un'] . ']核销';
				Db::name('hexiao_order')->insert($data);
				$remark = $order['remark'] ? $order['remark'] . ' ' . $data['remark'] : $data['remark'];

				$rs = \app\common\Order::collect($order, $type, $this->user['mid']);
				if ($rs['status'] == 0) {
					return $this->json($rs);
				}

				db($type . '_order')->where(['aid' => aid, 'hexiao_code' => $code])->update(['status' => 3, 'collect_time' => time(), 'remark' => $remark]);
				if (in_array($type, ['scoreshop', 'restaurant_shop', 'restaurant_takeaway'])) {
					Db::name($type . '_order_goods')->where(['aid' => aid, 'orderid' => $order['id']])->update(['status' => 3, 'endtime' => time()]);
				}
				if ($type == 'restaurant_shop') {
					$rs = \app\custom\Restaurant::shop_orderconfirm($order['id']);
					if ($rs['status'] == 0) {
						return $this->json($rs);
					}

				}
				if ($type == 'restaurant_takeaway') {
					$rs = \app\custom\Restaurant::takeaway_orderconfirm($order['id']);
					if ($rs['status'] == 0) {
						return $this->json($rs);
					}

				}
				if ($type == 'shop') {
					Db::name('shop_order_goods')->where(['aid' => aid, 'orderid' => $order['id']])->update(['status' => 3, 'endtime' => time()]);
					\app\common\Member::uplv(aid, $order['mid']);
				}
			} elseif ($type == 'coupon') {
				$data = array();
				$data['aid'] = aid;
				$data['bid'] = bid;
				$data['uid'] = $this->uid;
				$data['mid'] = $order['mid'];
				$data['orderid'] = $order['id'];
				$data['ordernum'] = date('ymdHis') . aid . rand(1000, 9999);
				$data['title'] = $order['couponname'];
				$data['type'] = $type;
				$data['createtime'] = time();
				$data['remark'] = '核销员[' . $this->user['un'] . ']核销';
				Db::name('hexiao_order')->insert($data);
				$remark = $order['remark'] ? $order['remark'] . ' ' . $data['remark'] : $data['remark'];
				if ($order['type'] == 3) {
					//计次券
					Db::name($type . '_record')->where(['aid' => aid, 'code' => $code])->inc('used_count', 1)->update();
					if ($order['used_count'] + 1 >= $order['limit_count']) {
						Db::name($type . '_record')->where(['aid' => aid, 'code' => $code])->update(['status' => 1, 'usetime' => time(), 'remark' => $remark]);
					}
				} else {
					Db::name($type . '_record')->where(['aid' => aid, 'code' => $code])->update(['status' => 1, 'usetime' => time(), 'remark' => $remark]);
					$record = Db::name($type . '_record')->where(['aid' => aid, 'code' => $code])->find();
					\app\common\Coupon::useCoupon(aid, $record['id'], 'hexiao');
				}
				\app\common\Wechat::updatemembercard(aid, $order['mid']);
			} elseif ($type == 'cycle') {

				$data = array();
				$data['aid'] = aid;
				$data['bid'] = bid;
				$data['uid'] = $this->uid;
				$data['mid'] = $order['mid'];
				$data['orderid'] = $order_state['id'];
				$data['ordernum'] = $order_state['ordernum'];
				$data['title'] = $order['title'];
				$data['type'] = $type;
				$data['createtime'] = time();
				$data['remark'] = '核销员[' . $this->user['un'] . ']核销';
				Db::name('hexiao_order')->insert($data);
				$remark = $order['remark'] ? $order['remark'] . ' ' . $data['remark'] : $data['remark'];

				db($type . '_order_stage')->where(['aid' => aid, 'hexiao_code' => $code])->update(['status' => 3, 'collect_time' => time(), 'remark' => $remark]);
				$order_stage_count = Db::name('cycle_order_stage')
					->where('status', 'in', '0,1,2')
					->where('orderid', $order['id'])
					->count();
				if ($order_stage_count == 0) {
					Db::name('cycle_order')->where('aid', aid)->where('bid', bid)->where('id', $order['id'])->update(['status' => 3, 'collect_time' => time()]);
					$rs = \app\common\Order::collect($order, $type, $this->user['mid']);
					if ($rs['status'] == 0) {
						return $this->json($rs);
					}

				} else {
					Db::name('cycle_order')->where('aid', aid)->where('bid', bid)->where('id', $order['id'])->update(['status' => 2]);
				}
			} elseif ($type == 'choujiang') {
				$data = array();
				$data['aid'] = aid;
				$data['bid'] = bid;
				$data['uid'] = $this->uid;
				$data['mid'] = $order['mid'];
				$data['orderid'] = $order['id'];
				$data['ordernum'] = date('ymdHis') . aid . rand(1000, 9999);
				$data['title'] = $order['jxmc'];
				$data['type'] = $type;
				$data['createtime'] = time();
				$data['remark'] = '核销员[' . $this->user['un'] . ']核销';
				Db::name('hexiao_order')->insert($data);
				$remark = $order['remark'] ? $order['remark'] . ' ' . $data['remark'] : $data['remark'];
				Db::name($type . '_record')->where(['aid' => aid, 'code' => $code])->update(['status' => 1, 'remark' => $remark]);
			} elseif ($type == 'shopproduct') {
				$data = array();
				$data['aid'] = aid;
				$data['bid'] = bid;
				$data['uid'] = $this->uid;
				$data['mid'] = $order['mid'];
				$data['orderid'] = $order['ogdata']['id'];
				$data['ordernum'] = $order['ordernum'];
				$data['title'] = $order['ogdata']['name'] . '(' . $order['ogdata']['ggname'] . ')';
				$data['type'] = $type;
				$data['createtime'] = time();
				$data['remark'] = '核销员[' . $this->user['un'] . ']核销';
				Db::name('hexiao_order')->insert($data);
				$remark = $order['remark'] ? $order['remark'] . ' ' . $data['remark'] : $data['remark'];
				Db::name('shop_order_goods')->where('id', $order['ogdata']['id'])->inc('hexiao_num', $order['hxnum'])->update(['hexiao_code' => random(18)]);

				$pdata = [];
				$pdata['aid'] = aid;
				$pdata['bid'] = bid;
				$pdata['uid'] = $this->uid;
				$pdata['mid'] = $order['mid'];
				$pdata['orderid'] = $order['ogdata']['id'];
				$pdata['ordernum'] = $order['ordernum'];
				$pdata['title'] = $order['ogdata']['name'] . '(' . $order['ogdata']['ggname'] . ')';
				$pdata['createtime'] = time();
				$pdata['remark'] = '核销员[' . $this->user['un'] . ']核销';
				$pdata['proid'] = $order['ogdata']['proid'];
				$pdata['name'] = $order['ogdata']['name'];
				$pdata['pic'] = $order['ogdata']['pic'];
				$pdata['ggid'] = $order['ogdata']['ggid'];
				$pdata['ggname'] = $order['ogdata']['ggname'];
				$pdata['num'] = $order['hxnum'];
				$pdata['ogid'] = $order['ogdata']['id'];
				Db::name('hexiao_shopproduct')->insert($pdata);

				if ($order['hxnum'] + $order['ogdata']['hexiao_num'] == $order['ogdata']['num']) {
					$totalhxnum = Db::name('shop_order_goods')->where('orderid', $order['id'])->sum('hexiao_num');
					$totalnum = Db::name('shop_order_goods')->where('orderid', $order['id'])->sum('num');
					if ($totalhxnum >= $totalnum) {
						Db::name('shop_order')->where('id', $order['id'])->update(['status' => 3, 'collect_time' => time(), 'remark' => '已核销']);
						Db::name('shop_order_goods')->where(['aid' => aid, 'orderid' => $order['id']])->update(['status' => 3, 'endtime' => time()]);
						\app\common\Member::uplv(aid, $order['mid']);
					}
				}
			} elseif ($type == 'yuyue') {
				$hexiao = Db::name('hexiao_order')->where('aid', aid)
					->where('bid', bid)->where('mid', $order['mid'])->where('orderid', $order['id'])->find();
				if ($hexiao) {
					return $this->json(['status' => 0, 'msg' => '该订单已核销']);
				}

				$data = array();
				$data['aid'] = aid;
				$data['bid'] = bid;
				$data['uid'] = $this->uid;
				$data['mid'] = $order['mid'];
				$data['orderid'] = $order['id'];
				$data['ordernum'] = date('ymdHis') . aid . rand(1000, 9999);
				$data['title'] = $order['proname'];
				$data['type'] = $type;
				$data['createtime'] = time();
				$data['remark'] = '核销员[' . $this->user['un'] . ']核销';
				Db::name('hexiao_order')->insert($data);

				// $pdata = [];
				// $pdata['aid'] = aid;
				// $pdata['bid'] = bid;
				// $pdata['uid'] = $this->uid;
				// $pdata['mid'] = $order['mid'];
				// $pdata['orderid'] = $order['id'];
				// $pdata['ordernum'] = $order['ordernum'];
				// $pdata['createtime'] = time();
				// $pdata['remark'] = '核销员['.$this->user['un'].']核销';
				// $pdata['name'] = $order['proname'];
				// $pdata['pic'] = $order['propic'];
				// $pdata['num'] = $order['num'];

				//    Db::name('hexiao_record')->insert($pdata);

				//更新订单状态
				Db::name('yuyue_order')->where('id', $order['id'])->update(['status' => 2]); //核销后，服务中

			}
			return $this->json(['status' => 1, 'msg' => '核销成功']);
		}
		return $this->json(['order' => $order, 'status' => 1]);
	}
	//核销记录
	public function record() {
		$pagenum = input('post.pagenum');
		if (!$pagenum) {
			$pagenum = 1;
		}

		$pernum = 20;
		$where = [];
		$where[] = ['order.aid', '=', aid];
		$where[] = ['order.bid', '=', bid];
		if ($this->user['mdid']) {
			$where[] = ['uid', '=', $this->user['id']];
		}
		if (input('param.keyword')) {
			$where[] = ['member.nickname', 'like', '%' . trim(input('param.keyword')) . '%'];
		}

		if (input('?param.status') && input('param.status') !== '') {
			$where[] = ['member_moneylog.status', '=', input('param.status')];
		}

		$datalist = Db::name('hexiao_order')->alias('order')->field('member.nickname,member.headimg,order.*')->join('member member', 'member.id=order.mid')->where($where)->page($pagenum, $pernum)->order('id desc')->select()->toArray();
		if ($pagenum == 1) {
			$count = 0 + Db::name('hexiao_order')->alias('order')->field('member.nickname,member.headimg,order.*')->join('member member', 'member.id=order.mid')->where($where)->count();
		}
		return $this->json(['status' => 1, 'count' => $count, 'data' => $datalist]);
	}
}